package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;

/**
 * Created by Administrator on 2016/10/13.
 */

public class EditAccountInfoDialog extends Dialog {

    private TextView country_code_tv;
    private EditText mEdit2;
    private TextView dialog_title_tv;
    private Context mContext;
    private EditAccountInforCallBack callBack;
    private int type;
    private String content;
    private String country;

    public EditAccountInfoDialog(Context context, int type, String country, String content, EditAccountInforCallBack callBack) {
        super(context);
        this.mContext = context;
        this.type = type;
        this.callBack = callBack;
        this.content = content;
        this.country = country;
    }

    public EditAccountInfoDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_edit_account_info);

        setDialogAttributes();
        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void initDialogView() {
        dialog_title_tv = (TextView) findViewById(R.id.dialog_title_tv);
        switch (type) {
            case 1:
                dialog_title_tv.setText(R.string.eidt_account_phone);
                break;
            default:
        }
        mEdit2 = (EditText) findViewById(R.id.myEdit2);
        if (content != null) {
            mEdit2.setText(content);
            mEdit2.setSelection(0, content.length());
        }


        country_code_tv = (TextView) findViewById(R.id.country_code_tv);
        if (!TextUtils.isEmpty(country)) {
            if (country.contains("00")) {
                country = country.replace("00", "+");
            } else {
                country = "+" + country;
            }
            country_code_tv.setText(country);
        }
        country_code_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ChooseCountryCodeDialog(mContext, new onCountryCodeChoosedListener() {
                    @Override
                    public void choosed(String countryCode) {
                        country_code_tv.setText(countryCode);
                    }
                }).show();
            }
        });
        findViewById(R.id.sureBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String phone = mEdit2.getText().toString().trim();
                    String countryCode = country_code_tv.getText().toString().trim();
                    countryCode = countryCode.replace("+", "00");
                    callBack.phoneCallBack(countryCode, phone);
                    dismiss();
                }
                return false;
            }
        });
        findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
